<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_right">
          <div>
            <el-button
              v-if="isAuth('module:business:activity:delete')"
              type="danger"
              icon="Delete"
              @click="deleteHandle()">
              {{ i18n('common.batch_delete') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_main_block_select">
        <div class="input">
          <el-select
            v-model="dataForm.organization_id"
            :placeholder="i18n('common.please_select') + i18n('organization.title')"
            @change="loadClientList" clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in organizationList" :label="v.title" :key="k" :value="v.id">
            </el-option>
          </el-select>
        </div>
        <div class="input">
          <el-select
            v-model="dataForm.client_id"
            :placeholder="i18n('common.please_select') + i18n('activity.client_id')"
            @change="loadContactList"
            clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in clientList" :label="v.name" :key="k" :value="v.id">
            </el-option>
          </el-select>
        </div>
        <div class="input">
          <el-select
            v-model="dataForm.contact_id"
            :placeholder="i18n('common.please_select') + i18n('activity.contact_id')"
            clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in contactList" :label="v.name" :key="k" :value="v.id">
            </el-option>
          </el-select>
        </div>
        <div class="input">
          <el-input v-model="dataForm.title" :placeholder="$t('common.please_input') + $t('activity.title')" clearable>
          </el-input>
        </div>
        <div class="input">
          <el-select v-model="dataForm.is_hidden" :placeholder="$t('common.please_select') + $t('common.is_hidden')" clearable>
            <el-option :label="$t('common.all')" value=""></el-option>
            <el-option :label="$t('common.hidden')" value="1"></el-option>
            <el-option :label="$t('common.show')" value="2"></el-option>
          </el-select>
        </div>
        <div class="date">
          <el-date-picker v-model="dataForm.end_time" type="daterange" :range-separator="$t('common.to')" :start-placeholder="$t('common.start_time')" :end-placeholder="$t('common.end_time')" clearable>
          </el-date-picker>
        </div>
        <div class="input">
          <el-button icon="Search" @click="getDataList(true)">
            {{ i18n('common.search') }}
          </el-button>
        </div>
      </div>

      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" stripe>
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" width="70">
          </el-table-column>

          <el-table-column :label="i18n('organization.title')" min-width="140">
            <template v-slot="scope">
              <span v-if="scope.row.organization">{{ scope.row.organization.title }}</span>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('activity.client_id')" min-width="150">
            <template v-slot="scope">
              <span v-if="scope.row.client">{{ scope.row.client.name }}</span>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('activity.contact_id')" min-width="100">
            <template v-slot="scope">
              <span v-if="scope.row.contact">{{ scope.row.contact.name }}</span>
            </template>
          </el-table-column>

          <el-table-column prop="title" :label="i18n('activity.title')" min-width="240">
          </el-table-column>

          <el-table-column :label="$t('common.is_hidden')" width="80">
            <template v-slot="scope">
              <el-switch
                v-model="scope.row.is_hidden.value"
                active-value="1"
                inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.yes')"
                :inactive-text="$t('common.no')"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                @change="handleStatus($event, scope.row.id, 'is_hidden')">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column prop="end_time" :label="i18n('activity.end_time')" width="100">
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="200">
            <template v-slot="scope">
              <el-button
                v-if="isAuth('module:business:activity:form')"
                type="primary"
                icon="Edit"
                @click="router.push({ name: 'module_business_activity_form', query: { id: scope.row.id } })">
                {{ i18n('common.update') }}
              </el-button>

              <el-button
                v-if="isAuth('module:business:activity:delete')"
                type="danger"
                icon="Delete"
                @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="business-activity-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/activity')

  const organizationList = ref([])
  const clientList = ref([])
  const contactList = ref([])

  const dataForm = reactive([
    'organization_id',
    'client_id',
    'contact_id',
    'title',
    'is_hidden',
    'end_time',
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    handleStatus,
    deleteHandle,
  } = useTable(model.value, dataForm)

  const loadOrganizationList = () => {
    $http({
      url: $http.adornUrl('/organization/select'),
      method: 'get',
    }).then(({ data }) => {
      if (data && data.status === 200) {
        organizationList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadClientList = (organization_id) => {
    $http({
      url: $http.adornUrl('/business/client/select'),
      method: 'get',
      params: $http.adornParams({
        organization_id: organization_id
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        clientList.value = data.data
        dataForm.client_id = ''
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadContactList = (client_id) => {
    $http({
      url: $http.adornUrl('/business/contact/select'),
      method: 'get',
      params: $http.adornParams({
        client_id: client_id
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        contactList.value = data.data
        dataForm.contact_id = ''
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    getDataList(model)

    loadOrganizationList()
  })
</script>
